package com.king.designpatterns;

import com.alibaba.druid.pool.DruidDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;

/**
 * @author king
 * @date 2020/9/28 14:21
 */
public class BaseDao {

    private static final Logger logger = LoggerFactory.getLogger(BaseDao.class);

    private static final String URL = "jdbc:mysql://127.0.0.1/student?serverTimezone=UTC";
    private static final String DRIVER_CLASS_NAME = "com.mysql.cj.jdbc.Driver";
    private static final String USERNAME = "root";
    private static final String PASSWORD = "root";
    private DruidDataSource dataSource;
    private Connection connection;

    public ResultSet executeQuery(String sql) {
        ResultSet resultSet = null;
        Statement statement = null;

        try {
            connection = getConnection();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
        } catch (SQLException e) {
            logger.error(e.getLocalizedMessage());
        }

        return resultSet;
    }

    public ResultSet executeQuery(String sql, String[] params) {
        ResultSet resultSet = null;
        PreparedStatement preparedStatement = null;

        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                preparedStatement.setString(i + 1, params[i]);
            }
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            logger.error(e.getLocalizedMessage());
        }

        return resultSet;
    }

    public boolean executeUpdate(String sql, String[] params) {
        PreparedStatement preparedStatement = null;
        int i = 0;

        try {
            connection = getConnection();
            preparedStatement = connection.prepareStatement(sql);
            for (int j = 0; j < params.length; j++) {
                preparedStatement.setString(j + 1, params[j]);
            }
            i = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            logger.error(e.getLocalizedMessage());
        }

        return i > 0;
    }

    /**
     * 初始化
     */
    private void init() {
        dataSource = new DruidDataSource();
        dataSource.setUrl(URL);
        dataSource.setDriverClassName(DRIVER_CLASS_NAME);
        dataSource.setUsername(USERNAME);
        dataSource.setPassword(PASSWORD);
    }

    /**
     * 获取连接对象
     *
     * @return 连接对象
     * @throws SQLException
     */
    private Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    /**
     * 关闭连接
     */
    public void closeConnection() {
        if (null != connection) {
            try {
                connection.close();
            } catch (SQLException e) {
                logger.error(e.getLocalizedMessage());
            }
        }
    }

    public BaseDao() {
        init();
    }
}
